package com.delamer.facile;

import com.delamer.utils.ItemUtil;
import com.delamer.utils.MsgUtil;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Executor {
    private Plugin plugin = Bukkit.getPluginManager().getPlugin("DelamerFacile");

    public void help(CommandSender sender, String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Bukkit.getPluginManager().getPlugin("DelamerFacile").getResource("main.help"), "UTF-8"));
        List<String> results = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null)
            results.add(line);
        sender.sendMessage(results.toArray(new String[]{}));
    }

    public void menu(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(Bukkit.getPluginManager().getPlugin("DelamerFacile").getDataFolder(), "menu-main.yml"));
        int size = config.getInt("menu.size");
        if (size < 1 || size > 6) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 尺寸配置错误"));
            return;
        } else {
            size *= 9;
        }
        Inventory inventory = Bukkit.createInventory(null, size, config.getString("menu.title"));
        ItemStack errorMenuItem = ItemUtil.createMenuItem(plugin, config.getString("menu.error.item").toUpperCase(), config.getString("menu.error.name"));
        if (errorMenuItem == null) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 填充栏配置错误:加载失败栏"));
            return;
        }
        ItemStack emptyMenuItem = ItemUtil.createMenuItem(plugin, config.getString("menu.empty.item").toUpperCase(), config.getString("menu.empty.name"));
        if (emptyMenuItem == null) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 填充栏配置错误：空栏"));
            return;
        }
        for (int i = 0; i < size; i++) {
            String prefix = "menu." + (i + 1);
            if (config.contains(prefix)) {
                ItemStack currentMenuItem = ItemUtil.createMenuItem(plugin, config.getString(prefix + ".item").toUpperCase(), config.getString(prefix + ".name"));
                if (currentMenuItem != null) {
                    if (config.contains(prefix + ".event")) {
                        Map<String, String> map = new HashMap<String, String>();
                        if (config.contains(prefix + ".event.all"))
                            map.put("AllClickEvent", config.getString(prefix + ".event.all"));
                        if (config.contains(prefix + ".event.left"))
                            map.put("LeftClickEvent", config.getString(prefix + ".event.left"));
                        if (config.contains(prefix + ".event.shift_left"))
                            map.put("ShiftLeftClickEvent", config.getString(prefix + ".event.shift_left"));
                        if (config.contains(prefix + ".event.right"))
                            map.put("RightClickEvent", config.getString(prefix + ".event.right"));
                        if (config.contains(prefix + ".event.shift_right"))
                            map.put("ShiftRightClickEvent", config.getString(prefix + ".event.shift_right"));
                        if (config.contains(prefix + ".event.middle"))
                            map.put("MiddleClickEvent", config.getString(prefix + ".event.middle"));
                        if (config.contains(prefix + ".event.drop"))
                            map.put("DropClickEvent", config.getString(prefix + ".event.drop"));
                        if (config.contains(prefix + ".event.ctrl_drop"))
                            map.put("CtrlDropClickEvent", config.getString(prefix + ".event.ctrl_drop"));
                        currentMenuItem = ItemUtil.addEvent(plugin, currentMenuItem, map);
                    }
                    if (config.contains(prefix + ".lore")) {
                        currentMenuItem = ItemUtil.addLore(plugin, currentMenuItem, config.getStringList(prefix + ".lore"));
                    }
                    inventory.setItem(i, currentMenuItem);
                } else {
                    inventory.setItem(i, errorMenuItem);
                }
            } else {
                inventory.setItem(i, emptyMenuItem);
            }
        }
        ((Player) sender).openInventory(inventory);
    }

}
